package com.xuebank.nfe.nfecar;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageButton;
import android.widget.SeekBar;
import android.widget.TextView;

import com.wzn.libaray.event.RxBus;
import com.xuebank.nfe.nfecar.activity.ReleaseActivity;
import com.xuebank.nfe.nfecar.config.PictureConfig;
import com.xuebank.nfe.nfecar.config.PictureMimeType;
import com.xuebank.nfe.nfecar.entity.EventEntity;
import com.xuebank.nfe.nfecar.entity.LocalMedia;
import com.xuebank.nfe.nfecar.globals.Constants;
import com.xuebank.nfe.nfecar.videoplayer.BaseMediaControllerHolder;
import com.xuebank.nfe.nfecar.videoplayer.BaseNativeVideoPlayerActivity;
import com.xuebank.nfe.nfecar.videoplayer.NativeMediaController;

import java.net.URI;
import java.util.ArrayList;
import java.util.List;

import rx.functions.Action1;


/**
 * 自定义UI视频播放器页面Demo。
 *
 * Created by Anchorer on 2014/9/23.
 */
    public class NativeVideoPlayerActivity extends BaseNativeVideoPlayerActivity implements NativeMediaController.MediaControllerGenerator {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 如果有通过Intent传入的其他数据，可以在该Activity的onCreate()方法中直接使用父类的Intent对象
        // ...
         final String videoId = getMIntent().getStringExtra("path");
//      Uri HAHA=   Uri.parse(videoId);


        RxBus.getDefault().toObserverable(String.class)
                .compose(this.<String>bindToLifecycle())
                .subscribe(new Action1<String>() {
                               @Override
                               public void call(String s) {
                                   if (s == "goFabu") {
                                       if (getIntent().getStringExtra(Constants.LOGIN_RELEASE) != null) {
                                           Intent intents = new Intent(NativeVideoPlayerActivity.this, ReleaseActivity.class);
                                           intents.putExtra("path",videoId);
                                           startActivity(intents);
                                           finish();

                                       } else if (getIntent().getStringExtra("isVideo")!=null) {
                                           Intent intent = new Intent();
                                           intent.putExtra("path",videoId);
                                           setResult(Activity.RESULT_OK,intent);
                                       } else {

                                           int duration = PictureMimeType.getLocalVideoDuration(videoId);
                                           List<LocalMedia> selectLists = new ArrayList<>();
                                           LocalMedia media = new LocalMedia();
                                           String videoType = PictureMimeType.createVideoType(videoId);
                                           media.setDuration(duration);
                                           media.setPath(videoId);
                                           media.setPictureType(videoType);

                                           selectLists.add(media);

                                           com.xuebank.nfe.nfecar.rxbus2.RxBus.getDefault().post(new EventEntity(PictureConfig.PREVIEW_DATA_FLAG, selectLists));
                                           finish();
                                           overridePendingTransition(0, R.anim.fade_out);
                                       }
                                   }
                               }
                           }
                );
        // ...

        // 如果有需要做一些监听的，可以使用父类中的MediaPlayer对象设置监听器
        // ...
        /*
        // 设置缓冲状态的监听器
        mPlayer.setOnInfoListener(new MediaPlayer.OnInfoListener() {
            @Override
            public boolean onInfo(MediaPlayer mp, int what, int extra) {
                return false;
            }
        });
        */
        // ...
    }
    @Override
    public BaseMediaControllerHolder generateMediaController() {
        LayoutInflater inflater = LayoutInflater.from(this);
        View view = inflater.inflate(R.layout.video_native_media_controler_custom, null);

        BaseMediaControllerHolder holder = new BaseMediaControllerHolder();
        holder.parentLayout = view;
        holder.pauseButton = (ImageButton) view.findViewById(R.id.video_native_media_controller_custom_btn_start);
        holder.currentTimeView = (TextView) view.findViewById(R.id.video_native_media_controller_custom_currenttime);
        holder.totalTimeView = (TextView) view.findViewById(R.id.video_native_media_controller_custom_totaltime);
        holder.mNext = (TextView) view.findViewById(R.id.video_native_media_next);

        holder.seekbar = (SeekBar) view.findViewById(R.id.video_native_media_controller_custom_seekbar);
        holder.fullScreenButton = (ImageButton) view.findViewById(R.id.video_native_media_controller_custom_btn_unfullscreen);
        holder.pauseResId = R.drawable.selector_video_btn_pause;
        holder.startResId = R.drawable.selector_video_btn_start;
        holder.fullscreenResId = R.drawable.selector_video_btn_fullscreen;
        holder.unfullscreenResId = R.drawable.selector_video_btn_unfullscreen;

        return holder;
    }
}
